home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 April / enter-2004-04.iso / files / httrack-3.30.exe / {app} / src / htsbase.h < prev    next >
Encoding:
C/C++ Source or Header  |  2003-10-11  |  10.3 KB  |  388 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: Basic definitions                                      */
  34. /*       Used in .c files for basic (malloc() ..) definitions   */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. #ifndef HTS_BASICH
  39. #define HTS_BASICH
  40.  
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44.  
  45. #include "htsglobal.h"
  46.  
  47. // size_t et mode_t
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50.  
  51. #ifdef HAVE_UNISTD_H
  52. #include <unistd.h>
  53. #endif
  54. #ifdef HAVE_SYS_TYPES_H
  55. #include <sys/types.h>
  56. #endif
  57. #ifdef HAVE_SYS_STAT_H
  58. #include <sys/stat.h>
  59. #endif
  60. #ifdef HAVE_DLFCN_H
  61. #include <dlfcn.h>
  62. #endif
  63.  
  64. #ifndef _WIN32
  65. #include <errno.h>
  66. #endif
  67.  
  68. #if HTS_WIN
  69. #else
  70. #include <fcntl.h>
  71. #endif
  72. #include <assert.h>
  73.  
  74. #undef min
  75. #undef max
  76. #define min(a,b) ((a)>(b)?(b):(a))
  77. #define max(a,b) ((a)>(b)?(a):(b))
  78.  
  79. // teste ΘgalitΘ de 2 chars, case insensitive
  80. #define hichar(a) ((((a)>='a') && ((a)<='z')) ? ((a)-('a'-'A')) : (a))
  81. #define streql(a,b) (hichar(a)==hichar(b))
  82.  
  83. // is this MIME an hypertext MIME (text/html), html/js-style or other script/text type?
  84. #define HTS_HYPERTEXT_DEFAULT_MIME "text/html"
  85. #define is_hypertext_mime(a) \
  86.    ( (strfield2((a),"text/html")!=0)\
  87.   || (strfield2((a),"application/x-javascript")!=0) \
  88.   || (strfield2((a),"text/css")!=0) \
  89.   /*|| (strfield2((a),"text/vnd.wap.wml")!=0)*/ \
  90.   || (strfield2((a),"image/svg+xml")!=0) \
  91.   || (strfield2((a),"image/svg-xml")!=0) \
  92.   /*|| (strfield2((a),"audio/x-pn-realaudio")!=0) */\
  93.   )
  94.  
  95. #define may_be_hypertext_mime(a) \
  96.    (\
  97.      (strfield2((a),"audio/x-pn-realaudio")!=0) \
  98.   )
  99.  
  100.  
  101. // caractΦre maj
  102. #define isUpperLetter(a) ( ((a) >= 'A') && ((a) <= 'Z') )
  103.  
  104. // functions
  105. #ifdef _WIN32
  106. #define DynamicGet(handle, sym) GetProcAddress(handle, sym)
  107. #else
  108. #define DynamicGet(handle, sym) dlsym(handle, sym)
  109. #endif
  110.  
  111. // emergency log
  112. typedef void (*t_abortLog)(char* msg, char* file, int line);
  113. extern HTSEXT_API t_abortLog abortLog__;
  114. #define abortLog(a) abortLog__(a, __FILE__, __LINE__)
  115. #define abortLogFmt(a) do { \
  116.   FILE* fp = fopen("CRASH.TXT", "wb"); \
  117.   if (!fp) fp = fopen("/tmp/CRASH.TXT", "wb"); \
  118.   if (!fp) fp = fopen("C:\\CRASH.TXT", "wb"); \
  119.   if (fp) { \
  120.     fprintf(fp, "HTTrack " HTTRACK_VERSIONID " closed at '" __FILE__ "', line %d\r\n", __LINE__); \
  121.     fprintf(fp, "Reason:\r\n"); \
  122.     fprintf(fp, a); \
  123.     fprintf(fp, "\r\n"); \
  124.     fflush(fp); \
  125.     fclose(fp); \
  126.   } \
  127. } while(0)
  128.  
  129.  
  130. #define _ ,
  131. #define abortLogFmt(a) do { \
  132.   FILE* fp = fopen("CRASH.TXT", "wb"); \
  133.   if (!fp) fp = fopen("/tmp/CRASH.TXT", "wb"); \
  134.   if (!fp) fp = fopen("C:\\CRASH.TXT", "wb"); \
  135.   if (fp) { \
  136.     fprintf(fp, "HTTrack " HTTRACK_VERSIONID " closed at '" __FILE__ "', line %d\r\n", __LINE__); \
  137.     fprintf(fp, "Reason:\r\n"); \
  138.     fprintf(fp, a); \
  139.     fprintf(fp, "\r\n"); \
  140.     fflush(fp); \
  141.     fclose(fp); \
  142.   } \
  143. } while(0)
  144. #define assertf(exp) do { \
  145.   if (! ( exp ) ) { \
  146.     abortLog("assert failed: " #exp); \
  147.     if (htsCallbackErr != NULL) { \
  148.       htsCallbackErr("assert failed: " #exp, __FILE__ , __LINE__ ); \
  149.     } \
  150.     assert(exp); \
  151.     abort(); \
  152.   } \
  153. } while(0)
  154. /* non-fatal assert */
  155. #define assertnf(exp) do { \
  156.   if (! ( exp ) ) { \
  157.     abortLog("assert failed: " #exp); \
  158.     if (htsCallbackErr != NULL) { \
  159.       htsCallbackErr("assert failed: " #exp, __FILE__ , __LINE__ ); \
  160.     } \
  161.   } \
  162. } while(0)
  163.  
  164.  
  165. /* regular malloc's() */
  166. #ifndef HTS_TRACE_MALLOC
  167. #define malloct(A)          malloc(A)
  168. #define calloct(A,B)        calloc((A), (B))
  169. #define freet(A)            do { assertnf((A) != NULL); if ((A) != NULL) { free(A); (A) = NULL; } } while(0)
  170. #define realloct(A,B)       ( ((A) != NULL) ? realloc((A), (B)) : malloc(B) )
  171. #define memcpybuff(A, B, N) memcpy((A), (B), (N))
  172. #else
  173. /* debug version */
  174. #define malloct(A)    hts_malloc(A)
  175. #define calloct(A,B)  hts_calloc(A,B)
  176. #define freet(A)      do { hts_free(A); (A) = NULL; } while(0)
  177. #define realloct(A,B) hts_realloc(A,B)
  178. void  hts_freeall();
  179. void* hts_malloc    (size_t);
  180. void* hts_calloc(size_t,size_t);
  181. void* hts_xmalloc(size_t,size_t);
  182. void  hts_free      (void*);
  183. void* hts_realloc   (void*,size_t);
  184. mlink* hts_find(char* adr);
  185. /* protected memcpy */
  186. #define memcpybuff(A, B, N) do { \
  187.   mlink* lnk = hts_find((void*)(A)); \
  188.   if (lnk != NULL) { \
  189.     assertf(lnk != NULL); \
  190.     assertf( * ( (t_htsboundary*) ( ((char*) lnk->adr) - sizeof(htsboundary) ) ) == htsboundary ); \
  191.     assertf( * ( (t_htsboundary*) ( ((char*) lnk->adr) + lnk->len ) ) == htsboundary ); \
  192.     assertf( ( ((char*)(A)) + (N)) < (char*) (lnk->adr + lnk->len) ); \
  193.   } \
  194.   memcpy(A, B, N); \
  195. } while(0)
  196.  
  197. #endif
  198.  
  199. typedef void (* htsErrorCallback)(char* msg, char* file, int line);
  200. extern HTSEXT_API htsErrorCallback htsCallbackErr;
  201. extern HTSEXT_API int htsMemoryFastXfr;
  202.  
  203. /*
  204. */
  205.  
  206.  
  207. #ifdef STRDEBUG
  208.  
  209. /* protected strcat, strncat and strcpy - definitely useful */
  210. #define strcatbuff(A, B) do { \
  211.   assertf( (A) != NULL ); \
  212.   if ( ! (B) ) { assertf( 0 ); } \
  213.   if (htsMemoryFastXfr) { \
  214.     if (sizeof(A) != sizeof(char*)) { \
  215.       (A)[sizeof(A) - 1] = '\0'; \
  216.     } \
  217.     strcat(A, B); \
  218.     if (sizeof(A) != sizeof(char*)) { \
  219.       assertf((A)[sizeof(A) - 1] == '\0'); \
  220.     } \
  221.   } else { \
  222.     unsigned int sz = (unsigned int) strlen(A); \
  223.     unsigned int szf = (unsigned int) strlen(B); \
  224.     if (sizeof(A) != sizeof(char*)) { \
  225.       assertf(sz + szf + 1 < sizeof(A)); \
  226.       if (szf > 0) { \
  227.         if (sz + szf + 1 < sizeof(A)) { \
  228.           memcpy((A) + sz, (B), szf + 1); \
  229.         } \
  230.       } \
  231.     } else if (szf > 0) { \
  232.       memcpybuff((A) + sz, (B), szf + 1); \
  233.     } \
  234.   } \
  235. } while(0)
  236. #define strncatbuff(A, B, N) do { \
  237.   assertf( (A) != NULL ); \
  238.   if ( ! (B) ) { assertf( 0 ); } \
  239.   if (htsMemoryFastXfr) { \
  240.     if (sizeof(A) != sizeof(char*)) { \
  241.       (A)[sizeof(A) - 1] = '\0'; \
  242.     } \
  243.     strncat(A, B, N); \
  244.     if (sizeof(A) != sizeof(char*)) { \
  245.       assertf((A)[sizeof(A) - 1] == '\0'); \
  246.     } \
  247.   } else { \
  248.     unsigned int sz = (unsigned int) strlen(A); \
  249.     unsigned int szf = (unsigned int) strlen(B); \
  250.     if (szf > (unsigned int) (N)) szf = (unsigned int) (N); \
  251.     if (sizeof(A) != sizeof(char*)) { \
  252.       assertf(sz + szf + 1 < sizeof(A)); \
  253.       if (szf > 0) { \
  254.         if (sz + szf + 1 < sizeof(A)) { \
  255.           memcpy((A) + sz, (B), szf); \
  256.           * ( (A) + sz + szf) = '\0'; \
  257.         } \
  258.       } \
  259.     } else if (szf > 0) { \
  260.       memcpybuff((A) + sz, (B), szf); \
  261.       * ( (A) + sz + szf) = '\0'; \
  262.     } \
  263.   } \
  264. } while(0)
  265. #define strcpybuff(A, B) do { \
  266.   assertf( (A) != NULL ); \
  267.   if ( ! (B) ) { assertf( 0 ); } \
  268.   if (htsMemoryFastXfr) { \
  269.     if (sizeof(A) != sizeof(char*)) { \
  270.       (A)[sizeof(A) - 1] = '\0'; \
  271.     } \
  272.     strcpy(A, B); \
  273.     if (sizeof(A) != sizeof(char*)) { \
  274.       assertf((A)[sizeof(A) - 1] == '\0'); \
  275.     } \
  276.   } else { \
  277.     unsigned int szf = (unsigned int) strlen(B); \
  278.     if (sizeof(A) != sizeof(char*)) { \
  279.       assertf(szf + 1 < sizeof(A)); \
  280.       if (szf > 0) { \
  281.         if (szf + 1 < sizeof(A)) { \
  282.           memcpy((A), (B), szf + 1); \
  283.         } else { \
  284.           * (A) = '\0'; \
  285.         } \
  286.       } else { \
  287.         * (A) = '\0'; \
  288.       } \
  289.     } else { \
  290.       memcpybuff((A), (B), szf + 1); \
  291.     } \
  292.   } \
  293. } while(0)
  294. #define strncpybuff(A, B, N) do { \
  295.   assertf( (A) != NULL ); \
  296.   if ( ! (B) ) { assertf( 0 ); } \
  297.   if (htsMemoryFastXfr) { \
  298.     if (sizeof(A) != sizeof(char*)) { \
  299.       (A)[sizeof(A) - 1] = '\0'; \
  300.     } \
  301.     strncpy(A, B, N); \
  302.     if (sizeof(A) != sizeof(char*)) { \
  303.       assertf((A)[sizeof(A) - 1] == '\0'); \
  304.     } \
  305.   } else { \
  306.     unsigned int szf = (unsigned int) strlen(B); \
  307.     if (szf > (unsigned int) (N)) szf = (unsigned int) (N); \
  308.     if (sizeof(A) != sizeof(char*)) { \
  309.       assertf(szf + 1 < sizeof(A)); \
  310.       if (szf > 0) { \
  311.         if (szf + 1 < sizeof(A)) { \
  312.           memcpy((A), (B), szf); \
  313.         } \
  314.       } \
  315.     } else { \
  316.       memcpybuff((A), (B), szf); \
  317.     } \
  318.   } \
  319. } while(0)
  320.  
  321. #else
  322.  
  323. #ifdef STRDEBUGFAST
  324.  
  325. /* protected strcat, strncat and strcpy - definitely useful */
  326. #define strcatbuff(A, B) do { \
  327.   assertf( (A) != NULL ); \
  328.   if ( ! (B) ) { assertf( 0 ); } \
  329.   if (sizeof(A) != sizeof(char*)) { \
  330.     (A)[sizeof(A) - 1] = '\0'; \
  331.   } \
  332.   strcat(A, B); \
  333.   if (sizeof(A) != sizeof(char*)) { \
  334.     assertf((A)[sizeof(A) - 1] == '\0'); \
  335.   } \
  336. } while(0)
  337. #define strncatbuff(A, B, N) do { \
  338.   assertf( (A) != NULL ); \
  339.   if ( ! (B) ) { assertf( 0 ); } \
  340.   if (sizeof(A) != sizeof(char*)) { \
  341.     (A)[sizeof(A) - 1] = '\0'; \
  342.   } \
  343.   strncat(A, B, N); \
  344.   if (sizeof(A) != sizeof(char*)) { \
  345.     assertf((A)[sizeof(A) - 1] == '\0'); \
  346.   } \
  347. } while(0)
  348. #define strcpybuff(A, B) do { \
  349.   assertf( (A) != NULL ); \
  350.   if ( ! (B) ) { assertf( 0 ); } \
  351.   if (sizeof(A) != sizeof(char*)) { \
  352.     (A)[sizeof(A) - 1] = '\0'; \
  353.   } \
  354.   strcpy(A, B); \
  355.   if (sizeof(A) != sizeof(char*)) { \
  356.     assertf((A)[sizeof(A) - 1] == '\0'); \
  357.   } \
  358. } while(0)
  359. #define strncpybuff(A, B, N) do { \
  360.   assertf( (A) != NULL ); \
  361.   if ( ! (B) ) { assertf( 0 ); } \
  362.   if (sizeof(A) != sizeof(char*)) { \
  363.     (A)[sizeof(A) - 1] = '\0'; \
  364.   } \
  365.   strncpy(A, B, N); \
  366.   if (sizeof(A) != sizeof(char*)) { \
  367.     assertf((A)[sizeof(A) - 1] == '\0'); \
  368.   } \
  369. } while(0)
  370.  
  371. #else
  372.  
  373. #define strcatbuff strcat
  374. #define strncatbuff strncat
  375. #define strcpybuff strcpy
  376. #define strncpybuff strncpy
  377.  
  378. #endif
  379.  
  380. #endif
  381.  
  382.  
  383. #ifdef __cplusplus
  384.  };
  385. #endif
  386.  
  387. #endif
  388.